// 方式四
// 从最长字符串开始往下循环找到最长回文字符串
// 时间：780ms
function longestPalindrome(s) {
  const len = s.length;
  if (len < 2) return s;
  let maxLenStr = "";
  for (let n = len - 1; n > -1; n--) {
    let palindromeStr = "";
    for (let m = 0; m < len - n; m++) {
      const str = s.substr(m, n + 1);
      if (isPalindrome(str)) {
        palindromeStr = str;
        break;
      }
    }
    if (palindromeStr) {
      maxLenStr = palindromeStr;
      break;
    }
  }

  return maxLenStr;

  function isPalindrome(x) {
    for (let i = 0, len = x.length; i < len / 2; i++) {
      if (x[i] != x[len - i - 1]) return false;
    }
    return true;
  }
}

console.log(longestPalindrome("1"));
